package team.seekers.hula.teamwork.team.teamUser.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.seekers.hula.common.entity.result.ApiExceptionEnum;
import team.seekers.hula.common.entity.result.ApiException;
import team.seekers.hula.teamwork.team.teamUser.entity.dao.TeamUserDO;
import team.seekers.hula.teamwork.team.teamUser.entity.vo.TeamUserListVO;
import team.seekers.hula.teamwork.team.teamUser.mapper.TeamUserMapper;
import team.seekers.hula.user.user.service.UserService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户和团队联系表 服务实现类
 *
 * @author yiming
 */
@Service
public class TeamUserService extends ServiceImpl<TeamUserMapper, TeamUserDO> {

    @Resource
    @Lazy
    private UserService userService;

    /**
     * 根据teamId查询teamUser，内部封装teamId及其对应的userId
     */
    public List<TeamUserListVO> getParticipantsByTeamId(String teamId) {
        List<TeamUserDO> teamUserDOS = lambdaQuery().eq(TeamUserDO::getTeamId,teamId).eq(TeamUserDO::getIsMonitor,0).list();

        ArrayList<TeamUserListVO> teamUserListVOS = new ArrayList<>();
        for (TeamUserDO teamUserDO : teamUserDOS) {
            TeamUserListVO teamUserListVO = new TeamUserListVO();
            BeanUtils.copyProperties(teamUserDO,teamUserListVO);
            teamUserListVO.setUserInfo(userService.getBasicInfoVO(teamUserDO.getUserId()));
            teamUserListVOS.add(teamUserListVO);
        }

        return teamUserListVOS;
    }

    /**
     * 根据团队id删除team_user中记录
     */
    @Transactional(rollbackFor = {RuntimeException.class})
    public void removeByTeamId(String teamId) {
        QueryWrapper<TeamUserDO> wrapper = new QueryWrapper<>();
        wrapper.eq("team_id", teamId);
        int delete = baseMapper.delete(wrapper);
        if(delete == 0){
            throw new ApiException(ApiExceptionEnum.DB_FAIL);
        }
    }

    public TeamUserListVO getMonitorInfoByTeamId(String teamId) {
        TeamUserDO teamUserDO = lambdaQuery().eq(TeamUserDO::getTeamId, teamId).eq(TeamUserDO::getIsMonitor, 1).one();

        TeamUserListVO teamUserListVO = new TeamUserListVO();
        BeanUtils.copyProperties(teamUserDO,teamUserListVO);
        teamUserListVO.setUserInfo(userService.getBasicInfoVO(teamUserDO.getUserId()));

        return teamUserListVO;
    }

    public List<String> getTeamIds(String userId) {
        List<TeamUserDO> list = lambdaQuery().select(TeamUserDO::getTeamId).eq(TeamUserDO::getUserId, userId).list();
        ArrayList<String> ids = new ArrayList<>();
        for (TeamUserDO teamUserDO : list) {
            ids.add(teamUserDO.getTeamId());
        }
        return ids;
    }

    public List<String> listMeInIds(String userId) {
        List<TeamUserDO> teamUserDOS = lambdaQuery().select(TeamUserDO::getTeamId).eq(TeamUserDO::getUserId, userId).list();
        ArrayList<String> ids = new ArrayList<>();
        for (TeamUserDO teamUserDO : teamUserDOS) {
            ids.add(teamUserDO.getTeamId());
        }
        return ids;
    }
}
